<%@ page import="java.util.HashMap"%>
<%@ page import="com.germinus.telcoblocks.echarts.Click2Dial.*"%>
<%@ page import="org.echarts.servlet.sip.EChartsMachineToJava" %>
<%@ page import="com.germinus.telcoblocks.echarts.db.HibernateUtil" %>
<%@ page import="com.germinus.telcoblocks.echarts.db.BindingExt" %>
<%@ page import="org.hibernate.Query" %>
<%@ page import="org.hibernate.Session" %>
<%@ page import="java.util.Iterator" %>
<%@ page import="java.util.List" %>




<%@page import="javax.servlet.sip.SipApplicationSession"%><html>
<title>Click To Call Demo</title>
<head>
<style>
body { font-family: Tahoma, Geneva, 'Trebuchet MS', Arial, Helvetica, sans-serif; }

.calltable, a {
	border: 1px solid #D4E0EE;
	border-collapse: collapse;
	font-family: "Trebuchet MS", Arial, sans-serif;
	color: #555;
}

.headtable {
	border: none;
	width: 100%;
}

td.calltable, tr {
	padding: 4px;
border: 1px dotted black;
}
</style>
</head>
<body>

<table class="headtable"><tr>
<a style="font-size: 30px;margin-left:100px"><b>Click-to-Call Sample Application based on ECharts</b></a></td>
</tr></table>
<hr/>
<%
		
		HashMap<String, String> users = new HashMap<String, String>();	
		Session sessionHibernate = HibernateUtil.getSessionFactory().getCurrentSession();
		sessionHibernate.beginTransaction();
		List<BindingExt> bindings = sessionHibernate.createQuery("FROM BindingExt b").list();
		//List<Call> callsList = (List<Call>) sessionHibernate.createQuery("FROM Call b where b.status='connected'").list();
		sessionHibernate.getTransaction().commit();
		Iterator<BindingExt> it = bindings.iterator();
		while (it.hasNext()) {
        BindingExt binding = (BindingExt) it.next();        	
        
        	users.put(binding.getAor(),binding.getContactAddress());
        
		}

        


if(users == null) {
	out.println("<a>No registered users. Please register at least two SIP User Agents.</a><br/>");
} else {
	String[] userArray = new String[0];
	userArray = users.keySet().toArray(userArray);
	//CallStatusContainer calls = (CallStatusContainer) getServletContext().getAttribute("activeCalls");
	CallStatusContainer calls = CallStatusContainer.getCallStatusContainer();
	 out.println("<table class=\"calltable\">");
        out.println("<tr><td bgcolor=\"#DDDDDD\">From \\ To</td>");
        for(String col:userArray) out.println("<td class=\"calltable\" bgcolor=\"#EEEEEE\"><b>" + col + "</b></td>");
        out.println("</tr>");
        for(String fromAddr:userArray) {
                out.println("<tr><td bgcolor=\"#EEEEEE\"><b>"+fromAddr +"</b></td>");
                String fromAddrV = users.get(fromAddr);
                for(String toAddr:userArray) {
                        if(!toAddr.equals(fromAddr)) {
                                String toAddrV = users.get(toAddr);
                                String status = calls==null? null:calls.getStatus(fromAddrV, toAddrV);
                                //String status = null;
                                
                                if(status == null) status = "FFFFFF"; // This is hex RGB color
                                if(status.equals("FFFFFF"))
                                        out.println("<td class=\"calltable\" bgcolor=\"#" + status
                                                + "\" align=\"center\"><a href=\"call?to="
                                                + users.get(toAddr) + "&from="
                                                + users.get(fromAddr) +"\"\">call</a></td>");
                                else
                                        out.println("<td class=\"calltable\" bgcolor=\"#" + "FFFF00"
                                                        + "\" align=\"center\"><a>call in progress </a><a href=\"call?to="
                                                        + users.get(toAddr) + "&bye=true&from="
                                                        + users.get(fromAddr) +"\"\">end</a></td>");

                        } else {
                                out.println("<td class=\"calltable\" ></td>");
                        }
                }
                out.println("<tr>");
        }
        out.println("</table>");
        out.println("<br/><a href=\"call?bye=all\">Close all calls</a>");
        
        
	
/*	out.println("<table class=\"calltable\">");
	out.println("<tr><td bgcolor=\"#DDDDDD\">From \\ To</td>");
	for(String col:userArray) out.println("<td class=\"calltable\" bgcolor=\"#EEEEEE\"><b>" + col + "</b></td>");
	out.println("</tr>");
	for(String fromAddr:userArray) {
		out.println("<tr><td bgcolor=\"#EEEEEE\"><b>"+fromAddr +"</b></td>");
		String fromAddrV = users.get(fromAddr);
		for(String toAddr:userArray) {
			if(!toAddr.equals(fromAddr)) {
				String toAddrV = users.get(toAddr);
				out.println("<td class=\"calltable\" bgcolor=\"#" + "FFFFFF"
						+ "\" align=\"center\"><a href=\"call?to="
						+ users.get(toAddr) + "&from="
						+ users.get(fromAddr) +"\"\">call</a></td>");
				
			} else {
				out.println("<td class=\"calltable\" ></td>");
			}
		}
		out.println("<tr>");
	}
	out.println("</table>");
	out.println("<br/><a href=\"call?bye=all\">Close all calls</a>");*/
}

%>
</body>
</html>
